/* -*- c++ -*-
*
* postlist.h
*
* Copyright (C) 2005 Petter Stokke <ummo@hellokitty.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef DELICIOUS_POSTLIST_H
#define DELICIOUS_POSTLIST_H

#include <klistview.h>

#include <qtooltip.h>
#include <qevent.h>
#include <qpixmap.h>

#include "bookmarkapi.h"

class PostListItem;

class PostList : public KListView, QToolTip
{
    Q_OBJECT
    Q_PROPERTY( QStringList tagFilter READ tagFilter WRITE setTagFilter )
    Q_PROPERTY( QString searchFilter READ searchFilter WRITE setSearchFilter )
public:
    PostList( BookmarkAPI* api, QWidget* parent = 0, const char* name = "PostList" );

    PostListItem* currentPost() const;
    const QStringList& tagFilter() const
    {
        return m_tagFilter;
    }
    const QString& searchFilter() const
    {
        return m_searchFilter;
    }

    BookmarkAPI* api() const
    {
        return m_api;
    }

    bool newWindowByDefault() const
    {
        return m_newWindowByDefault;
    }
    void setNewWindowByDefault( bool _newWindowByDefault )
    {
        m_newWindowByDefault = _newWindowByDefault;
    }

public slots:
    void setTagFilter( const QStringList& tags );
    void setSearchFilter( const QString& query );
    void updatePosts( Collection* collection );

signals:
    void openPostDefaultAction( Post* post );
    void openPost( Post* post );
    void openPostInNewWindow( Post* post );
    void editPost( Post* post );
    void changedNewWindowByDefault( bool newValue );
    void urlDropped( const KURL& url, const QStringList& tags );
    void submitURL( const KURL& url, const QStringList& tags );
    void titleChanged( const QString& title );

protected:
    void maybeTip( const QPoint& p );
    QString tipText( Post* p );
    bool acceptDrag( QDropEvent* event ) const;
    void setPosts( const QPtrList<Post>& _posts );

protected slots:
    void slotContextMenu( KListView*, QListViewItem*, const QPoint& );
    void slotOpenCurrentPostDefaultAction();
    void slotOpenCurrentPost();
    void slotOpenCurrentPostInNewWindow();
    void slotCopyURL();
    void slotEditCurrentPost();
    void slotToggleDefaultAction();
    void slotDropped( QDropEvent* event );
    void slotPostCurrentPost();
    void slotThumbJobResult( KIO::Job* );

private:
    BookmarkAPI *m_api;
    Collection* m_posts;
    QString m_searchFilter;
    QStringList m_tagFilter;
    bool m_newWindowByDefault;
    Post *m_tipFor;
    QRect m_tipRect;
    QString m_tipThumb;
    QMap<QString, bool> m_fetchedThumbs;

};

class PostListItem : public KListViewItem
{
public:
    PostListItem( PostList* parent, Post* _post );

    Post* post() const;
    void setPost( Post* _post );

    QString text( int column ) const;
    const QPixmap* pixmap( int column ) const;

    void updateFavicon();

private:
    Post* m_post;
    QPixmap m_favicon;
};

#endif
